package net.sxnic.normal.action;

import java.io.File;

import javax.servlet.ServletContext;

import net.sxnic.comm.CommConstant;
import net.sxnic.comm.basecode.BaseCodeManager;
import net.sxnic.comm.property.PropertyManager;
import net.sxnic.comm.utils.CommDataImportUtils;
import net.sxnic.item.catalog.CatalogManager;
import net.sxnic.item.clpv.ClpvManager;
import net.sxnic.item.duiying.DuiyingManager;
import net.sxnic.item.mdata.MdataManager;
import net.sxnic.item.zzdw.ZzdwManager;
import net.sxnic.jhitem.itemoperation.ItemOperationManager;
import net.sxnic.normal.util.ItemImportUtils;
import net.sxnic.normal.util.MenuImportUtils;
import net.sxnic.normal.util.PermissionUtils;
import net.sxnic.normal.util.UgrImportUtils;
import net.sxnic.ugr.UgrActionSupport;
import net.sxnic.ugr.dept.DeptManager;
import net.sxnic.ugr.dept.util.DeptImportUtils;
import net.sxnic.ugr.group.GroupManager;
import net.sxnic.ugr.menu.MenuManager;
import net.sxnic.ugr.permission.PermissionManager;
import net.sxnic.ugr.resource.ResourceManager;
import net.sxnic.ugr.resource.util.ResourceUtils;
import net.sxnic.ugr.role.RoleManager;
import net.sxnic.ugr.security.AuthorizationManager;
import net.sxnic.ugr.user.UserManager;
import net.sxnic.ugr.website.WebsiteManager;
import net.sxnic.workflow.node.NodeManager;
import net.sxnic.workflow.pd.PdManager;
import net.sxnic.workflow.pd.utils.PdUtils;
import net.sxnic.workflow.task.TaskManager;
import net.sxnic.workflow.transition.TransitionManager;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Introduction ：系统根据所选模块进行设置
 * 
 * @author 孙宇飞
 * @version 2009-3-5
 */
@Controller
public class FirstRunSetting extends UgrActionSupport {

	private static final Log log = LogFactory.getLog(FirstRunSetting.class);

	@Autowired
	private UserManager userManager;
	@Autowired
	private AuthorizationManager authorizationManager;
	@Autowired
	private BaseCodeManager basecodeManager;
	@Autowired
	private PropertyManager propertyManager;
	@Autowired
	private MenuManager menuManager;
	@Autowired
	private GroupManager groupManager;
	@Autowired
	private DeptManager deptManager;
	@Autowired
	private PermissionManager permissionManager;
	@Autowired
	private ResourceManager resourceManager;
	@Autowired
	private RoleManager roleManager;
	@Autowired
	private NodeManager nodeManager;
	@Autowired
	private PdManager pdManager;
	@Autowired
	private TransitionManager trsManager;
	@Autowired
	private WebsiteManager wsManager;
	@Autowired
	private DuiyingManager dyManager;
	@Autowired
	private ZzdwManager zzdwManager;
	@Autowired
	private ClpvManager clpvManager;
	@Autowired
	private ItemOperationManager operationManager;
	@Autowired
	private CatalogManager ctManager;
	@Autowired
	private MdataManager mdManager;
	@Autowired
	private TaskManager taskManager;
	@Autowired
	private ServletContext servletContext;

	@RequestMapping(value = "/normal/firstRunSetting.jspa", method = RequestMethod.POST)
	public String submit(String app, ModelMap model) {		
		// 系统初始化
		try {
			String initFilePath = servletContext.getRealPath("/") + "WEB-INF/init/";
			// 收文流程
			File file = null;

			// pd
			if (StringUtils.contains(app, "pd")) {
				PdUtils.readxml(new File(servletContext.getRealPath("/")
						+ "config/linfenitem/" + "wf_config.xml"), pdManager,
						nodeManager, trsManager);
			}
			// 初始化基本码表
			if (StringUtils.contains(app, "basecode")) {
				file = new File(initFilePath + CommConstant.INIT_BASECODE_TXT);
				CommDataImportUtils.initBaseCode(basecodeManager, file);
			}
			// 初始化属性表
			if (StringUtils.contains(app, "property")) {
				file = new File(initFilePath + CommConstant.INIT_PROPERTY_TXT);
				CommDataImportUtils.initProperty(propertyManager, file);
			}
			// 初始化菜单
			if (StringUtils.contains(app, "menu")) {
				file = new File(initFilePath + CommConstant.INIT_MENU_TXT);
				MenuImportUtils.importMenu(menuManager, file);
			}
			// 初始化部门
			if (StringUtils.contains(app, "dept")) {
				// 部门信息
				DeptImportUtils.deptImport(deptManager, initFilePath
						+ "dept.txt");
			}

			if (StringUtils.contains(app, "admin")) {
				// 初始管理员用户admin和一个测试用户test1
				UgrImportUtils.initAdminUser(userManager, deptManager,
						authorizationManager);
			}

			// 用户-组-角色 信息
			if (StringUtils.contains(app, "ugr")) {
				UgrImportUtils.importUserRoleGroup(authorizationManager,
						groupManager, userManager, permissionManager,
						resourceManager, deptManager, wsManager, mdManager,
						taskManager, initFilePath + "ugr.txt");
			}

			if (StringUtils.contains(app, "resource")) {
				file = new File(initFilePath + "resource.txt");
				ResourceUtils.readResourceFromFile(file, resourceManager);
			}

			if (StringUtils.contains(app, "permission")) {
				file = new File(initFilePath + "permission.txt");
				PermissionUtils.readPermissionFromFile(file, permissionManager,
						resourceManager, roleManager);
			}

			if (StringUtils.contains(app, "item")) {
				file = new File(initFilePath + "item.txt");
				ItemImportUtils.importItem(ctManager, clpvManager, zzdwManager,
						deptManager, dyManager, operationManager, initFilePath
								+ "item.txt");
			}
			


			log.debug("============system first run, init base data success!====================");

		} catch (Exception e) {
			e.printStackTrace();

			return INPUT;
		}

		return SUCCESS;
	}
}
